package com.tiancikeji.zaoke.app.dao.token.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.tiancikeji.zaoke.app.dao.token.TokenDao;
import com.tiancikeji.zaoke.app.model.token.Token;

public class TokenDaoImpl extends JdbcDaoSupport implements TokenDao{

	public Set<String> getAllTokens(){
		final Set<String> result = new HashSet<String>();
		
		getJdbcTemplate().query("select distinct token from zaoke.token", new RowMapper<Integer>() {

			@Override
			public Integer mapRow(ResultSet rs, int index) throws SQLException {
				result.add(rs.getString("token"));
				return index;
			}
		});
		
		return result;
	}
	
	public void insert(Token token){
		getJdbcTemplate().update("insert into zaoke.token(user_id, token) values(?,?)", new Object[]{token.getUserid(), token.getToken()});
	}
}
